package GPSTracker;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;


/**
 * @author thomas.s
 */
public class GPSTrackerMain extends MIDlet implements CommandListener {

    Command okCmd;
    Command menuCmd;
    Command exitCmd;
    Display display;
    GPSTrackerView viewer;
    GPSTrackerThread gpx;
    GPSTrackerMenu menu;

    public GPSTrackerMain()
    {
        display = Display.getDisplay (this);
        gpx = new GPSTrackerThread();
        menu = new GPSTrackerMenu(display,gpx,this);
        viewer = new GPSTrackerView (display,gpx,menu);
        okCmd = new Command ("OK", Command.OK, 1);
        menuCmd = new Command (menu.getText("Menu"), Command.ITEM, 1);
        exitCmd = new Command (menu.getText("Exit"), Command.EXIT, 1);

        menu.setViewer(viewer);
        viewer.addCommand( menuCmd );
        viewer.addCommand (exitCmd);
        viewer.setCommandListener (this);
        menu.addCommand( okCmd );
        menu.addCommand( exitCmd );
        menu.setCommandListener(this);
    }

    public void startApp() {
        display.setCurrent(viewer);
        gpx.createLocator();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        gpx.closeGPX();
        menu.close();
    }

    public void commandAction (Command c, Displayable s) {
        if (c == exitCmd ) {
            destroyApp (true);
            notifyDestroyed ();
        }
        else if (c == menuCmd) menu.setActive();
        else if (c == okCmd ) viewer.setActive();
    }

}
